<extend name="Public:base" />
<block name="title">食堂档口管理</block>
<block name="content">
    <style>
        .inputItem {
            text-indent: 14px;
            width: 120px !important;
        }
        
        .border1 {
            border: 1px solid #ddd;
        }
        
        .canteenContent {
            width: 100%;
            
            display: flex;
            flex-wrap: wrap;
        }
        
        .canteenItem {
            height: 40px;
            line-height: 30px;
            margin-left: 10px;
            /*flex-basis: 200px;*/
            padding: 10px;
            font-size: 16px;
        }
        
        .canteenItemHint {
            text-indent: 10px;
            margin-top: 10px;
            margin-bottom: 10px;
            
        }
        
        .alert {
            color: orange;
        }
        
        .janbtn {
            width: 240px;
            height: 60px;
            background: #0a6ebd;
            line-height: 60px;
            border-radius: 5px;
            color: white;
            text-align: center;
            margin: 0 auto;
            font-size: 20px;
            cursor: pointer;
        }
        
        .janbtn:hover {
            background: rgba(10, 110, 189, 0.62);
            
        }
    
    </style>
    <div class=" ace-save-state" id="breadcrumbs">
        <div class="page-header">
            <h1>订单管理
                <small><i class="ace-icon fa fa-angle-double-right"></i>订单总汇</small>
                <small><i class="ace-icon fa fa-angle-double-right"></i><?php echo $order_date;?>订单详情</small>
            </h1>
        </div>
        <div class="page-content col-xs-11">
            <div class="row">
                <div class="col-xs-12">
                    <div class="alert alert-block alert-success">
                        
                        <i class="ace-icon fa fa-check green"></i>
                        <strong class="green">
                            填写菜品当天单价后请保存金额，该单价会自动显示在各个档口的订单中
                            <small></small>
                        </strong>
                    
                    </div>
                    <div class="canteenContent">
                        
                        <?php if(!empty($canteenList)){?>
                        <?php foreach($canteenList as $canteen){?>
                        <div class="infobox infobox-green infobox-small infobox-dark center">
                            <div class="infobox-data">
                                <div class="infobox-content">
                                    <h5><?php echo $canteen['canteen_name']?></h5>
                                </div>
                                <div class="infobox-content" style="padding-top: 5px">
                                    <h4><?php echo $canteen['book']?> | <?php echo $canteen['total']?></h4></div>
                            </div>
                        </div>
                        <?php }?>
                        <?php }else{?>
                        <div class="col-sm-12" style="padding: 0">
                            <div class="alert alert-danger">
                                <button type="button" class="close" data-dismiss="alert">
                                    <i class="ace-icon fa fa-times"></i>
                                </button>
                                
                                <strong>
                                    <i class="ace-icon fa fa-times"></i>
                                </strong>
                                
                                未添加饭堂、档口，请添加后再操作
                                <br>
                            </div>
                        </div>
                        <?php }?>
                    </div>
                    
                    <?php if(!empty($itemList)){?>
                    <div style="margin-top: 10px;margin-bottom: 10px">
                        <button onclick="showAllWindowOrder()" class="btn btn-sm btn-default"><i class="fa fa-sticky-note-o"></i> 档口订单列表</button>
                        <button onclick="showNoOrder()" class="btn btn-sm btn-inverse">未提交订单档口列表</button>
                        <br>
                        <br>
                        <button onclick="makeCaigou()" class="btn btn-sm btn-info">
                            <i class="fa fa-sticky-note-o"></i> 导出采购单
                        </button>
                        <button onclick="makePeiSong()" class="btn btn-sm btn-info">
                            <i class="fa fa-truck"></i> 导出配菜单
                        </button>
                        <button onclick="makeSongCai()" class="btn btn-sm btn-info">
                            <i class="fa fa-truck"></i> 导出送菜单
                        </button>
                       
                    </div>
                    <?php }else{?>
                    
                    <?php }?>
                    <div class="col-xs-3" style="padding: 0;margin-bottom: 10px;margin-top: 10px">
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-calendar-times-o"></i></span>
                            <input type="text" id="id-date-picker-1" value="<?php echo $order_date?>" class="form-control date-picker search-query" data-date-format="yyyy-mm-dd" placeholder="选择日期">
                            <span class="input-group-btn">
                                <button onclick="gogogo()" type="button" class="btn btn-inverse btn-white">
                                    <span class="ace-icon fa fa-search icon-on-right bigger-110"></span>
                                    查询订单
                                </button>
                            </span>
                        </div>
                    </div>
                    
                    
                    <div class="row" style="margin-top: 20px">
                        <div class="col-xs-12">
                            <table id="simple-table2" class="table  table-bordered table-hover">
                                <thead>
                                <tr>
                                    <th class="detail-col center">菜品名称</th>
                                    <th class="center">规格</th>
                                    <th class="hidden-480 center">结算价</th>
                                    <th class="hidden-480 center">采购价</th>
                                    <th class="hidden-480 center">数量</th>
                                    <th class="hidden-480 center">合计金额(结算)</th>
                                    <th class="hidden-480 center">合计金额(采购)</th>
                                </tr>
                                </thead>
                                
                                <tbody>
                                <form id="order-form" method="post" action="{:U('/Admin/Order/saveOrderPrice/')}">
                                    <input type="hidden" name="orderDate" value="<?php echo $order_date?>">
                                    <?php if(!empty($itemList)){?>
                                    <?php foreach($itemList as $k=>$item){ if($item['item_number']==0)continue;?>
                                    <tr>
                                        <td class="center"><?php echo $item['item_name'];?></td>
                                        <td class="center"><?php echo $item['unit_name'];?></td>
                                        <td class="hidden-480 center">
                                            <input type="number" class="inputItem" style="width: 220px;" min="0.01" placeholder="销售价" jan="price" value="<?php echo $item['item_price']?>" k="<?php echo $k;?>" name="itemList[<?php echo $item['item_id']?>][price]">
                                        </td>
                                        <td class="hidden-480 center">
                                            <input type="number" class="inputItem" style="width: 220px;" min="0.01" placeholder="采购价" jan="buy_price" value="<?php echo $item['item_buy_price']?>" k="<?php echo $k;?>" name="itemList[<?php echo $item['item_id']?>][buy_price]">
                                        </td>
                                        <td class="hidden-480 center" id="number<?php echo $k;?>"><?php echo $item['item_number'];?></td>
                                        <td class="hidden-480 center" jan="total" id="total<?php echo $k;?>">0</td>
                                        <td class="hidden-480 center" jan="buy_total" id="buy_total<?php echo $k;?>">0</td>
                                    </tr>
                                    <?php }?>
                                    <tr>
                                        <th class="center" colspan="5"></th>
                                        <th class="hidden-480 center" id="orderTotal">0</th>
                                        <th class="hidden-480 center" id="orderBuyTotal">0</th>
                                    </tr>
                                    
                                    <?php }else{?>
                                    <tr>
                                        <td style="text-align: center;font-size: 20px" colspan="8">没有找到相关的信息</td>
                                    </tr>
                                    <?php }?>
                                </form>
                                
                                
                                </tbody>
                            </table>
                        </div><!-- /.span -->
                    </div><!-- /.row -->
                    <?php if(!empty($itemList)){?>
                    <div class="col-sm-3" style="margin:  0 auto">
                        <button class="btn btn-danger btn-block" onclick="saveOrder()">保存</button>
                    </div>
                    <?php }?>
                </div>
            </div>
        </div>
    </div>
    <div class="modal fade" id="windowOrderList" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> &times;</button>
                    <h4 class="modal-title" id="myModalLabel"> 档口订单列表</h4></div>
                <div class="modal-body">
                    <h4>以下列表为「{$order_date}」目前已经提交订单的档口</h4><br>
                    <h5>未提交订单的档口不在此处显示</h5><br>
                    <table class="table table-striped table-bordered table-hover table-condensed">
                        <tr>
                            <th>档口名称：</th>
                            <th>所属饭堂</th>
                            <th></th>
                        </tr>
                        <?php if(!empty($memberList)){?>
                        <?php foreach($memberList as $member){?>
                        <tr>
                            <th><?php echo $member['window_name']?></th>
                            <td><?php echo $member['canteen_name']?></td>
                            <td>
                                <button type="button" class="btn btn-minier btn-primary" onclick="showOrder(<?php echo $member['member_id']?>)">
                                    <span>查看/修改档口订单</span>
                                </button>
                            </td>
                        </tr>
                        <?php }?>
                        <?php }else{?>
                        <tr>
                            <td colspan="3">没有找到相关信息或未有档口提交订单</td>
                        </tr>
                        <?php }?>
                    
                    </table>
                </div>
            </div>
        </div>
    </div>
    
    <script>
        window.onload = function () {
            $("input[jan=price]").each(function () {
                var price = $(this).val()
                var k = $(this).attr("k")
                var number = $("#number" + k).html()
                $("#total" + k).html(keepTwoDecimalFull(price * number))
                getTotal()

            })
            $("input[jan=buy_price]").each(function () {
                var price = $(this).val()
                var k = $(this).attr("k")
                var number = $("#number" + k).html()
                $("#buy_total" + k).html(keepTwoDecimalFull(price * number))
                getBuyTotal()

            })
            $("input[jan=price]").on('keyup', function (e) {
                var price = $(this).val()
                var k = $(this).attr("k")
                var number = $("#number" + k).html()
                $("#total" + k).html(keepTwoDecimalFull(price * number))
                getTotal()
            })
            $("input[jan=buy_price]").on('keyup', function (e) {
                var price = $(this).val()
                var k = $(this).attr("k")
                var number = $("#number" + k).html()
                $("#buy_total" + k).html(keepTwoDecimalFull(price * number))
                getBuyTotal()
            })
            $('.date-picker').datepicker({
                autoclose: true,
                todayHighlight: true
            })
        }

        function showOrder(member_id) {
            var date = $('#id-date-picker-1').val()
            window.location.href = "{:U('Admin/Order/WindowOrder')}" + "/member_id/" + member_id+"/order_date/"+date
        }

        function showAllWindowOrder() {
            $('#windowOrderList').modal('show');
        }

        function makeCaigou() {
            var flag = confirm("生成采购单前，请确保当天菜品已保存单价,是否继续？")
            if (flag == true) {
                var date = $('#id-date-picker-1').val()
                window.location.href = "{:U('Admin/Order/makeCaigou/')}" + "?order_date=" + date
            }

        }

        function makePeiSong() {
            var flag = confirm("生成采配送单前，请确保当天菜品已保存单价,是否继续？")
            if (flag == true) {
                var date = $('#id-date-picker-1').val()
                window.location.href = "{:U('Admin/Order/makePeiSong/')}" + "?order_date=" + date
            }
        }
        function makeSongCai() {
            var flag = confirm("生成送菜单前，请确保当天菜品已保存单价,是否继续？")
            if (flag == true) {
                var date = $('#id-date-picker-1').val()
                window.location.href = "{:U('Admin/Order/makeSongCai/')}" + "?order_date=" + date
            }
        }

        function showNoOrder() {
            var date = $('#id-date-picker-1').val()
            window.location.href = "{:U('Admin/Order/noOrder/')}" + "/order_date/" + date
        }

        function saveOrder() {
            //验证
            var full = 0
            var makesense = 0
            $("input[jan=price]").each(function () {
                full++
                if ($(this).val()) {
                    makesense++
                }
            })
            if (makesense == full) {
                $("#order-form").submit()
            } else {
                alert("请填写所有菜品的单价")
                return false
            }


        }

        function isIntNum(val) {
            var regPos = / ^\d+$/; // 非负整数
            var regNeg = /^\-[1-9][0-9]*$/; // 负整数
            if (regPos.test(val) || regNeg.test(val)) {
                return true;
            } else {
                return false;
            }
        }

        function getTotal() {
            var total = 0
            $("td[jan=total]").each(function () {
                total = total + Number($(this).html())

            })

            $("#orderTotal").html(keepTwoDecimalFull(total))
        }

        function getBuyTotal() {
            var total = 0
            $("td[jan=buy_total]").each(function () {
                total = total + Number($(this).html())

            })

            $("#orderBuyTotal").html(keepTwoDecimalFull(total))
        }

        function keepTwoDecimalFull(num) {
            var result = parseFloat(num);
            if (isNaN(result)) {
                alert('传递参数错误，请检查！');
                return false;
            }
            result = Math.round(num * 100) / 100;
            var s_x = result.toString();
            var pos_decimal = s_x.indexOf('.');
            if (pos_decimal < 0) {
                pos_decimal = s_x.length;
                s_x += '.';
            }
            while (s_x.length <= pos_decimal + 2) {
                s_x += '0';
            }
            return s_x;
        }

        function gogogo() {
            var date = $('#id-date-picker-1').val()
            window.location.href = "{:U('Admin/Order/index/')}" + "?order_date=" + date
        }
    
    </script>
</block>
<block name="js"></block>
